home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 1
/
Cream of the Crop 1.iso
/
PROGRAM
/
CBGRX100.ARJ
/
FONTCVT.C
< prev
next >
Wrap
Text File
|
1992-01-10
|
9KB
|
392 lines
/*
* cvtfont.c
*/
#include "grx.h"
#include "grxfile.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
short *width_table = NULL;
char **bitmap = NULL;
char *workline = NULL;
char name[100];
char family[100];
long bitmapsize;
int inwidth;
int inheight;
int outwidth;
int outheight;
int xpad1 = 0;
int xpad2 = 0;
int ypad1 = 0;
int ypad2 = 0;
int minchar;
int maxchar;
int isfixed;
int numchars;
int verbose;
int wantfixed;
int center_h = 0;
int center_w = 0;
FILE *infile;
FILE *outfile;
#define ASC2BIN 0
#define BIN2ASC 1
#define MAKEDIR 2
int make_bitmap(int w,int h)
{
static int last_w = -1;
static int last_h = -1;
char *area = NULL;
int ii;
if((w <= last_w) && (h <= last_h)) return(1);
if(workline != NULL) free(workline);
if(bitmap != NULL) free(bitmap);
bitmap = malloc(sizeof(char *) * h);
area = malloc(w * (h + 1));
if((bitmap == NULL) || (area == NULL)) return(0);
workline = area;
for(ii = 0; ii < h; ii++) bitmap[ii] = (area += w);
last_w = w;
last_h = h;
return(1);
}
char *getline(char *buff)
{
char *p = buff;
int chr;
while((chr = getc(infile)) != EOF) {
if(chr == '\n') {
*p = '\0';
if((buff[0] == '\0') || (buff[0] == ';')) {
p = buff;
continue;
}
return(buff);
}
*p++ = chr;
}
return(NULL);
}
int read_header(void)
{
char *p;
char param[100],value[100],lnbuff[200];
int have_params = 0;
while(have_params < 5) {
if((p = getline(lnbuff)) == NULL) return(0);
if(sscanf(p,"%s %s",param,value) < 2) return(0);
if(strcmp(param,"family") == 0)
strcpy(family,value);
else if(strcmp(param,"width") == 0)
inwidth = atoi(value);
else if(strcmp(param,"height") == 0)
inheight = atoi(value);
else if(strcmp(param,"minchar") == 0)
minchar = atoi(value);
else if(strcmp(param,"maxchar") == 0)
maxchar = atoi(value);
else return(0);
have_params++;
}
return(1);
}
void write_header(void)
{
FntFileHdr fhdr;
memset(&fhdr,0,sizeof(fhdr));
fhdr.magic = FONT_MAGIC;
fhdr.bitmapsize = bitmapsize;
fhdr.h.fnt_isfixed = isfixed;
fhdr.h.fnt_width = inwidth;
fhdr.h.fnt_height = inheight;
fhdr.h.fnt_minchar = minchar;
fhdr.h.fnt_maxchar = maxchar;
fhdr.h.fnt_internal = 0;
strncpy(fhdr.h.fnt_name,name,GR_NAMEWIDTH);
strncpy(fhdr.h.fnt_family,family,GR_NAMEWIDTH);
fhdr.h.fnt_name[GR_NAMEWIDTH - 1] = '\0';
fhdr.h.fnt_family[GR_NAMEWIDTH - 1] = '\0';
fwrite(&fhdr,sizeof(fhdr),1,outfile);
}
void write_table(void)
{
fwrite(width_table,sizeof(short),numchars,outfile);
}
int read_asciimap(void)
{
int ii,jj;
char *p,lnbuff[200];
for(ii = 0; ii < inheight; ii++) {
if((p = getline(lnbuff)) == NULL) return(0);
for(jj = 0; jj < inwidth; jj++) {
switch(*p++) {
case '#': bitmap[ii][jj] = 1; break;
case '.': bitmap[ii][jj] = 0; break;
default: return(0);
}
}
}
return(1);
}
void center_width(void)
{
int fmin,fmax;
int offs,copy;
int ii,jj;
fmax = 0;
fmin = outwidth;
for(ii = 0; ii < outheight; ii++) {
for(jj = 0; jj < outwidth; jj++) {
if(bitmap[ii][jj]) {
if(fmax < jj) fmax = jj;
if(fmin > jj) fmin = jj;
}
}
}
copy = fmax - fmin + 1;
if(copy <= 0) return;
offs = (outwidth - copy) / 2;
for(ii = 0; ii < outheight; ii++) {
memset(workline,0,outwidth);
memcpy(&workline[offs],&bitmap[ii][fmin],copy);
memcpy(bitmap[ii],workline,outwidth);
}
}
void transform_bitmap(void)
{
/* if(outwidth != inwidth) pad_width();
if(outheight != inheight) pad_height(); */
if(center_w) center_width();
}
int write_bitmap(void)
{
int ii,jj,kk,wdt;
int bits,mask;
for(ii = 0; ii < numchars; ii++) {
if(!read_asciimap()) return(0);
transform_bitmap();
wdt = isfixed ? outwidth : width_table[ii];
for(jj = 0; jj < outheight; jj++) {
bits = 0;
mask = 0x100;
for(kk = 0; kk < wdt; kk++) {
if((mask >>= 1) == 0) {
putc(bits,outfile);
bits = 0;
mask = 0x80;
}
if(bitmap[jj][kk]) bits |= mask;
}
putc(bits,outfile);
}
}
return(1);
}
int convert_font(char *inname)
{
char outname[200];
char *p;
if((infile = fopen(inname,"r")) == NULL) {
fprintf(stderr,"error opening input file: %s\n",inname);
return(0);
}
if(!read_header()) {
fprintf(stderr,"invalid input file: %s\n",inname);
fclose(infile);
return(0);
}
if(!make_bitmap(inwidth,inheight)) {
fprintf(stderr,"insufficient memory\n");
fclose(infile);
exit(1);
}
outwidth = inwidth;
outheight = inheight;
numchars = maxchar - minchar + 1;
bitmapsize =
(long)numchars * (long)(((outwidth + 7) >> 3) * outheight);
strcpy(outname,inname);
p = strrchr(outname,'.');
if(p != NULL) *p = '\0';
strcat(outname,".fnt");
if((outfile = fopen(outname,"wb")) == NULL) {
fprintf(stderr,"error creating output file: %s\n",outname);
fclose(infile);
return(0);
}
if(verbose) printf(" converting font: %s to %s\n",inname,outname);
p = strrchr(outname,'\\');
if(p == NULL) p = strrchr(outname,'/');
if(p == NULL) p = strrchr(outname,':');
if(p == NULL) p = outname;
strcpy(name,outname);
for(p = name; *p != '\0'; p++) *p = tolower(*p);
write_header();
if(!write_bitmap()) {
fprintf(stderr,"error reading input file: %s\n",inname);
fclose(infile);
fclose(outfile);
unlink(outname);
return(0);
}
fclose(infile);
if(ferror(outfile)) {
fprintf(stderr,"error writing output file: %s\n",outname);
fclose(outfile);
unlink(outname);
return(0);
}
fclose(outfile);
return(1);
}
int createdir(char *name,int numfonts)
{
FntDirHdr hdr;
if((outfile = fopen(name,"wb")) == NULL) {
fprintf(stderr,"error creating directory file: %s\n",name);
return(0);
}
if(verbose) printf("creating font directory: %s\n",name);
hdr.magic = FONTDIR_MAGIC;
hdr.numentries = numfonts;
fwrite(&hdr,sizeof(hdr),1,outfile);
return(1);
}
int addentry(char *name)
{
FntFileHdr fhdr;
if((infile = fopen(name,"rb")) == NULL) {
fprintf(stderr,"error opening input file: %s\n",name);
return(0);
}
if((fread(&fhdr,sizeof(fhdr),1,infile) != 1) ||
(fhdr.magic != FONT_MAGIC)) {
fprintf(stderr,"invalid input file: %s\n",name);
return(0);
}
if(verbose) printf(" adding font: %s\n",name);
fwrite(&fhdr.h,sizeof(GrFont),1,outfile);
fclose(infile);
return(1);
}
void usage(void)
{
fprintf(stderr,"usage: fontdisp [<options>] font [-o output] ...\n");
exit(1);
}
void main(int argc,char **argv)
{
int ii;
char *p;
char **fonts,**fp;
char **result,**rp;
char *dirname;
int numfonts;
int operation;
isfixed = 1;
verbose = 0;
wantfixed = 1;
operation = ASC2BIN;
fonts = malloc(sizeof(char *) * (argc + 1));
result = malloc(sizeof(char *) * (argc + 1));
if((fonts == NULL) || (result == NULL)) {
fprintf(stderr,"insufficient memory\n");
exit(1);
}
numfonts = 0;
fp = fonts;
rp = result;
for(ii = 1; ii < argc; ii++) {
if(*(p = argv[ii]) == '-') {
if(strcmp(p,"-v") == 0)
verbose = 1;
else if(strcmp(p,"-u") == 0)
operation = BIN2ASC;
else if(strcmp(p,"-cw") == 0)
center_w = 1;
else if(strcmp(p,"-ch") == 0)
center_h = 1;
else if(strcmp(p,"-d") == 0) {
operation = MAKEDIR;
dirname = "fonts.dir";
}
else if(strncmp(p,"-do",3) == 0) {
operation = MAKEDIR;
if(p[3] != '\0') p += 3;
else if(++ii == argc) usage();
else p = argv[ii];
dirname = p;
}
else if(strncmp(p,"-o",2) == 0) {
if(rp == result) usage();
if(p[2] != '\0') p += 2;
else if(++ii == argc) usage();
else p = argv[ii];
rp[-1] = p;
}
else usage();
}
else {
*fp++ = p;
*rp++ = NULL;
numfonts++;
}
}
switch(operation) {
case ASC2BIN:
for(ii = 0; ii < numfonts; ii++)
if(!convert_font(fonts[ii])) exit(1);
break;
case MAKEDIR:
if(!createdir(dirname,numfonts)) exit(1);
for(ii = 0; ii < numfonts; ii++) {
if(!addentry(fonts[ii])) {
fclose(outfile);
unlink(dirname);
exit(1);
}
}
if(ferror(outfile)) {
fprintf(stderr,"error writing directory file: %s\n",dirname);
fclose(outfile);
unlink(dirname);
exit(1);
}
break;
}
exit(0);
}